package com.project.timemanage.activities;

import android.text.TextUtils;
import android.view.View;

import androidx.databinding.ViewDataBinding;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.project.timemanage.App;
import com.project.timemanage.R;
import com.project.timemanage.databinding.ActivityChangePlanBinding;
import com.project.timemanage.entity.Plan;

import org.greenrobot.eventbus.EventBus;
import org.xutils.ex.DbException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.project.base.baseui.BaseActivity;
import cn.project.base.utils.Event;

public class ChangePlanActivity extends BaseActivity {
    private ActivityChangePlanBinding binding;
    private Plan plan;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_change_plan;
    }

    @Override
    protected void setDataBinding(ViewDataBinding dataBinding) {
        binding = (ActivityChangePlanBinding) dataBinding;
    }

    @Override
    protected void initView() {
        binding.toolbar.tvTitle.setText("修改用药记录");
        plan = (Plan) getIntent().getSerializableExtra("detail");
        binding.etContent.setText(plan.getContent());
        binding.etTitle.setText(plan.getTitle());
    }

    @Override
    protected void initListener() {
        binding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        binding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = binding.etTitle.getText().toString();
                String content = binding.etContent.getText().toString();
                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)){
                    showToast("信息不完整");
                    return;
                }
                plan.setContent(content);
                plan.setTitle(title);
                try {
                    App.dbManager.update(plan);
                    showToast("修改计划成功");
                    finish();
                } catch (DbException e) {
                    throw new RuntimeException(e);
                }

            }
        });
    }
    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().postSticky(new Event("ADD_PLAN"));
    }
}
